using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using ClimbTracker.BusinessLogic.Entities;
using MySql.Data.MySqlClient;
using System.IO;

namespace ClimbTracker.Reports
{
    public partial class RoutesReportForm2 : Form
    {
        public RoutesReportForm2()
        {
            InitializeComponent();
            
            
            // Create datasets and table adapter
            ClimbTracker.Reports.RoutesReport2 rep = new ClimbTracker.Reports.RoutesReport2();
            this.routesTableAdapter1.Fill(this.climbTrackerDataSet1.routes);


            // Enables presenting images in the report
            for (int index = 0; index < climbTrackerDataSet1.routes.Rows.Count; index++)
            {
                if (climbTrackerDataSet1.routes.Rows[index]["PicturePath"].ToString() != "")
                {
                    Capture cap = new Capture(climbTrackerDataSet1.routes.Rows[index]["PicturePath"].ToString());
                    string s = "c:\\temp.jpg";
                    //cap.QueryFrame().Rotate(ClimbTracker.GUI.TabControls.RoutesTabControl.
                    double rotationAngle = double.Parse(climbTrackerDataSet1.routes.Rows[index]["RotationAngle"].ToString());
                    cap.QueryFrame().Rotate(rotationAngle, new Bgr(Color.Black), false).Save(s);
                    //string s = climbTrackerDataSet1.routes.Rows[index]["PicturePath"].ToString();

                    if (File.Exists(s))
                    {
                        LoadImage(climbTrackerDataSet1.routes.Rows[index], "imagePath", s);
                    }
                    else
                    {
                        LoadImage(climbTrackerDataSet1.routes.Rows[index], "imagePath",
                                  "DefaultPicturePath");
                    }
                }
                else
                {
                    LoadImage(climbTrackerDataSet1.routes.Rows[index], "imagePath",
                              "DefaultPicturePath");
                }
            }

            try
            {
                // Finally display report in crystal report viewer
                rep.Load("RoutesReport2.rpt");
                rep.SetDataSource(climbTrackerDataSet1);
                crystalReportViewer1.ReportSource = rep;
            }
            catch (Exception ex)
            {
                throw;
            }

            rep.SetDataSource(climbTrackerDataSet1);
            crystalReportViewer1.ReportSource = rep;

        }


        private void LoadImage(DataRow objDataRow, string strImageField, string FilePath)
        {
            try
            {
                FileStream fs = new FileStream(FilePath,
                           System.IO.FileMode.Open, System.IO.FileAccess.Read);
                byte[] Image = new byte[fs.Length];
                fs.Read(Image, 0, Convert.ToInt32(fs.Length));
                fs.Close();
                objDataRow[strImageField] = Image;
            }
            catch (Exception ex)
            {
                //Response.Write("<font color=red>" + ex.Message + "</font>");
            }
        }
    }

}